<!DOCTYPE html>
<html>
<head>
    <title>Electric Potential vs. Position Graph</title>
</head>
<body>
<canvas id="physics-canvas" width="700" height="500" style="border:1px solid #000;"></canvas>

<script>
    const canvas = document.getElementById('physics-canvas');
    const ctx = canvas.getContext('2d');

    // Define coordinate system parameters
    const originX = 320;
    const originY = 250;
    const scaleX = 35;
    const scaleY = 15;

    // Helper function to transform graph coordinates to canvas coordinates
    function T(x, y) {
        return { x: originX + x * scaleX, y: originY - y * scaleY };
    }

    // Clear canvas
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    
    // Set drawing styles
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 1.5;
    ctx.font = '16px Arial';
    ctx.fillStyle = 'black';

    // Draw axes
    ctx.beginPath();
    // X-axis
    ctx.moveTo(50, originY);
    ctx.lineTo(650, originY);
    ctx.lineTo(640, originY - 5);
    ctx.moveTo(650, originY);
    ctx.lineTo(640, originY + 5);
    // Y-axis
    ctx.moveTo(originX, 450);
    ctx.lineTo(originX, 50);
    ctx.lineTo(originX - 5, 60);
    ctx.moveTo(originX, 50);
    ctx.lineTo(originX + 5, 60);
    ctx.stroke();

    // Draw axis labels
    ctx.textAlign = 'left';
    ctx.textBaseline = 'top';
    ctx.fillText('V/V', originX + 10, 50);
    ctx.textAlign = 'right';
    ctx.textBaseline = 'top';
    ctx.fillText('x/m', 640, originY + 10);

    // Draw ticks and labels on axes
    ctx.lineWidth = 1;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'top';

    // X-axis ticks
    for (let i = -8; i <= 8; i++) {
        const pos = T(i, 0);
        if (i === 0) {
            ctx.fillText('O', pos.x, pos.y + 5);
            continue;
        }
        ctx.beginPath();
        ctx.moveTo(pos.x, pos.y);
        const tickHeight = (Math.abs(i) === 5) ? 8 : 4;
        ctx.lineTo(pos.x, pos.y - tickHeight);
        ctx.stroke();
        if (Math.abs(i) === 5) {
            ctx.fillText(i.toString(), pos.x, pos.y + 5);
        }
    }

    // Y-axis ticks
    ctx.textAlign = 'right';
    ctx.textBaseline = 'middle';
    for (let i = -12; i <= 12; i += 6) {
        if (i === 0) continue;
        const pos = T(0, i);
        ctx.beginPath();
        ctx.moveTo(pos.x, pos.y);
        ctx.lineTo(pos.x + 8, pos.y);
        ctx.stroke();
        ctx.fillText(i.toString(), pos.x - 5, pos.y);
    }

    // Define graph points
    const points = {
        a: { x: -7, y: 0 },
        b: { x: -5, y: 12 },
        c: { x: -2, y: 12 },
        d: { x: 0, y: 6 },
        e: { x: 2, y: 0 },
        f: { x: 3, y: -6 },
        g: { x: 5, y: -6 },
        h: { x: 7, y: 0 }
    };

    // Draw the graph line
    ctx.beginPath();
    ctx.lineWidth = 2.5;
    let p = T(points.a.x, points.a.y);
    ctx.moveTo(p.x, p.y);
    p = T(points.b.x, points.b.y);
    ctx.lineTo(p.x, p.y);
    p = T(points.c.x, points.c.y);
    ctx.lineTo(p.x, p.y);
    p = T(points.e.x, points.e.y);
    ctx.lineTo(p.x, p.y);
    p = T(points.f.x, points.f.y);
    ctx.lineTo(p.x, p.y);
    p = T(points.g.x, points.g.y);
    ctx.lineTo(p.x, p.y);
    p = T(points.h.x, points.h.y);
    ctx.lineTo(p.x, p.y);
    ctx.stroke();

    // Draw dashed lines
    ctx.beginPath();
    ctx.lineWidth = 1;
    ctx.strokeStyle = '#333';
    ctx.setLineDash([4, 4]);

    // From b to x-axis
    let p_b_start = T(points.b.x, points.b.y);
    let p_b_end = T(points.b.x, 0);
    ctx.moveTo(p_b_start.x, p_b_start.y);
    ctx.lineTo(p_b_end.x, p_b_end.y);

    // From c to x-axis
    let p_c_start = T(points.c.x, points.c.y);
    let p_c_end = T(points.c.x, 0);
    ctx.moveTo(p_c_start.x, p_c_start.y);
    ctx.lineTo(p_c_end.x, p_c_end.y);

    // From f to x-axis
    let p_f_start = T(points.f.x, points.f.y);
    let p_f_end = T(points.f.x, 0);
    ctx.moveTo(p_f_start.x, p_f_start.y);
    ctx.lineTo(p_f_end.x, p_f_end.y);

    // From g to x-axis
    let p_g_start = T(points.g.x, points.g.y);
    let p_g_end = T(points.g.x, 0);
    ctx.moveTo(p_g_start.x, p_g_start.y);
    ctx.lineTo(p_g_end.x, p_g_end.y);

    ctx.stroke();
    ctx.setLineDash([]); // Reset dash
    ctx.strokeStyle = 'black'; // Reset color

    // Draw point labels
    ctx.fillStyle = 'black';
    ctx.font = 'italic 18px Times New Roman';

    function placeLabel(p, label, dx, dy) {
        const pos = T(p.x, p.y);
        ctx.fillText(label, pos.x + dx, pos.y + dy);
    }

    placeLabel(points.a, 'a', -15, -5);
    placeLabel(points.b, 'b', -10, -20);
    placeLabel(points.c, 'c', 5, -20);
    placeLabel(points.d, 'd', 10, -10);
    placeLabel(points.e, 'e', 5, -20);
    placeLabel(points.f, 'f', -5, 15);
    placeLabel(points.g, 'g', 5, 15);
    placeLabel(points.h, 'h', 10, -5);

    // Draw title
    ctx.font = '20px "SimSun", "Songti SC"';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';
    ctx.fillText('习题 7-33 图', canvas.width / 2, canvas.height - 10);
</script>

</body>
</html>